require 'test_helper'


class ClientsControllerTest < ActionController::TestCase
  setup do
    @irc_client = clients(:irc)
    @id         = @irc_client.id
    @secret     = @irc_client.secret
  end

  def post_client client_params
    post :create , :client => client_params
  end

  def post_message client_id , client_secret , message_params
    post :message , :id => client_id , :secret => client_secret , :message => message_params
  end


  ##  client creation tests ##

  test "should not create client with missing room name" do
    assert_no_difference('Client.count') { post_client CLIENT_PARAMS_MISSING_ROOM  }
    assert_response 420
  end

  test "should not create client with blank room name" do
    assert_no_difference('Client.count') { post_client CLIENT_PARAMS_BLANK_ROOM    }
    assert_response 420
  end

  test "should not create client with missing nicks CSV" do
    assert_no_difference('Client.count') { post_client CLIENT_PARAMS_MISSING_NICKS }
    assert_response 420
  end

  test "should not create client with blank nicks CSV" do
    assert_no_difference('Client.count') { post_client CLIENT_PARAMS_BLANK_NICKS   }
    assert_response 420
  end

  test "should not create client with missing client type" do
    assert_no_difference('Client.count') { post_client CLIENT_PARAMS_MISSING_TYPE  }
    assert_response 420
  end

  test "should not create client with blank client type" do
    assert_no_difference('Client.count') { post_client CLIENT_PARAMS_BLANK_TYPE    }
    assert_response 420
  end

  test "should create client with valid params" do
    assert_difference('Client.count')    { post_client CLIENT_PARAMS_VALID         }

    assert_not_nil assigns :client
    assert_response        :success
  end


  ##  message creation tests ##

  test "should not create message with missing nick" do
    assert_no_difference('Message.count') { post_message @id , @secret  , MESSAGE_PARAMS_MISSING_NICK }
    assert_response 420
  end

  test "should not create message with blank nick" do
    assert_no_difference('Message.count') { post_message @id , @secret  , MESSAGE_PARAMS_BLANK_NICK   }
    assert_response 420
  end

  test "should not create message with missing text" do
    assert_no_difference('Message.count') { post_message @id , @secret  , MESSAGE_PARAMS_MISSING_TEXT }
    assert_response 420
  end

  test "should not create message with blank text" do
    assert_no_difference('Message.count') { post_message @id , @secret  , MESSAGE_PARAMS_BLANK_TEXT   }
    assert_response 420
  end

  test "should not create message with invalid client id" do
    assert_no_difference('Message.count') { post_message 42  , @secret  , MESSAGE_PARAMS_VALID        }
    assert_response 420
  end

  test "should not create message with incorrect client secret" do
    assert_no_difference('Message.count') { post_message @id , 'secret' , MESSAGE_PARAMS_VALID        }
    assert_response 420
  end

  test "should create message with valid params" do
    assert_difference('Message.count')    { post_message @id , @secret  , MESSAGE_PARAMS_VALID        }
    assert_response :success
  end


  ##  message fetch tests ##

  test "should not fetch messages with invalid client id" do
    get :messages , :id => 42  , :secret => @secret
    assert_response 420
  end

  test "should not fetch messages with incorrect client secret" do
    get :messages , :id => @id , :secret => 'secret'
    assert_response 420
  end

  test "should fetch messages with valid params" do
    get :messages , :id => @id , :secret => @secret
    assert_response :success
  end
end
